/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.model.retail;

import com.xtd.model.employment.Employee;
import com.xtd.model.settlement.Form;
import com.xtd.util.FormTypeEnum;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.IndexColumn;

/**
 *
 * @author Lu Yuanbin
 */
@Entity
@Table(name = "shooting_order")
@NamedQueries({
    @NamedQuery(name = "ShootingOrder.findAll", query = "SELECT o FROM ShootingOrder o")})
public class ShootingOrder extends Form {

    private static final long serialVersionUID = 1L;
    @Column(name = "mname")
    private String mname;
    @Column(name = "fname")
    private String fname;
    @Column(name = "mphone")
    private String mphone;
    @Column(name = "fphone")
    private String fphone;
    @Column(name = "shot_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date shotTime;
    @Column(name = "select_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date selectTime;
    @Column(name = "get_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date getTime;
    @Column(name = "marriage_date")
    @Temporal(TemporalType.DATE)
    private Date marriageDate;
    @Column(name = "shot_amount")
    private int shotAmount;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "shootingOrder")
    private Collection<OrderDressSuit> orderDressSuitCollection;
    @JoinColumn(name = "account_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Account account;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @IndexColumn(name = "order_commodity_position", base = 0)
    @JoinColumn(name = "order_id")
    private List<OrderCommodity> orderCommodityCollection;
    @JoinColumn(name = "introducer", referencedColumnName = "id")
    @ManyToOne
    private Employee introducer;
    @JoinColumn(name = "principle", referencedColumnName = "id")
    @ManyToOne
    private Employee principle;

    public boolean isNew() {
        if (getId() == null) {
            return true;
        } else {
            return false;
        }
    }

    public ShootingOrder() {
        account = new Account();
    }

    public ShootingOrder(String formNumber, Employee createdBy) {
        super(formNumber,FormTypeEnum.摄影);
        account = new Account();
      

        this.setCreatedBy(createdBy);
        this.setCreatedTime(new Date());
        this.principle = createdBy;


    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getMphone() {
        return mphone;
    }

    public void setMphone(String mphone) {
        this.mphone = mphone;
    }

    public String getFphone() {
        return fphone;
    }

    public void setFphone(String fphone) {
        this.fphone = fphone;
    }

    public Date getShotTime() {
        return shotTime;
    }

    public void setShotTime(Date shotTime) {
        this.shotTime = shotTime;
    }

    public Date getSelectTime() {
        return selectTime;
    }

    public void setSelectTime(Date selectTime) {
        this.selectTime = selectTime;
    }

    public Date getGetTime() {
        return getTime;
    }

    public void setGetTime(Date getTime) {
        this.getTime = getTime;
    }

    public Date getMarriageDate() {
        return marriageDate;
    }

    public void setMarriageDate(Date marriageDate) {
        this.marriageDate = marriageDate;
    }

    public Collection<OrderDressSuit> getOrderDressSuitCollection() {
        return orderDressSuitCollection;
    }

    public void setOrderDressSuitCollection(Collection<OrderDressSuit> orderDressSuitCollection) {
        this.orderDressSuitCollection = orderDressSuitCollection;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public List<OrderCommodity> getOrderCommodityCollection() {
        if (orderCommodityCollection == null) {
            orderCommodityCollection = new ArrayList();
        }
        return orderCommodityCollection;
    }

    public void setOrderCommodityCollection(List<OrderCommodity> orderCommodityCollection) {
        this.orderCommodityCollection = orderCommodityCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ShootingOrder)) {
            return false;
        }
        ShootingOrder other = (ShootingOrder) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.getId().equals(other.getId()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.xtd.model.Order1[id=" + getId() + "]";
    }

    /**
     * @return the shotAmount
     */
    public int getShotAmount() {
        return shotAmount;
    }

    /**
     * @param shotAmount the shotAmount to set
     */
    public void setShotAmount(int shotAmount) {
        this.shotAmount = shotAmount;
    }

    /**
     * @return the introducer
     */
    public Employee getIntroducer() {
        return introducer;
    }

    /**
     * @param introducer the introducer to set
     */
    public void setIntroducer(Employee introducer) {

        this.introducer = introducer;
    }

    /**
     * @return the totalAmount
     */
    @Override
    public BigDecimal getTotalAmount() {
        BigDecimal amount = BigDecimal.ZERO;
        if (orderCommodityCollection != null) {
            for (OrderCommodity oc : orderCommodityCollection) {
                amount = amount.add(oc.getUnitPrice().multiply(BigDecimal.valueOf(oc.getQuantity() - oc.getReturnedQuantity())));
            }
        }
        return amount;
    }

    public boolean isBalanced() {
        return (getTotalAmount().compareTo(getTotalPayment().subtract(getTotalExpense())) == 0);
    }

    public boolean hasStockoutCommodity() {
        for (OrderCommodity oc : getOrderCommodityCollection()) {
            if (oc.isStock()) {
                if (oc.getStockout() > 0) {
                    return true;
                }
            }
            for (OrderCommodity subItem : oc.getSubOrderCommodityCollection()) {
                if (subItem.isStock()) {
                    if (oc.getStockout() > 0) {
                        return true;
                    }
                }
                for (OrderCommodity material : subItem.getSubOrderCommodityCollection()) {
                    if (material.getStockout() > 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * @return the principle
     */
    public Employee getPrinciple() {
        return principle;
    }

    /**
     * @param principle the principle to set
     */
    public void setPrinciple(Employee principle) {
        this.principle = principle;
    }

    public boolean isUnStockCommodity() {
        for (OrderCommodity oc : getOrderCommodityCollection()) {
            if (oc.isStock()) {
                if (oc.getQuantity() - oc.getReturnedQuantity() != oc.getStockout()) {
                    return true;
                }
            }
            for (OrderCommodity subItem : oc.getSubOrderCommodityCollection()) {
                if (subItem.isStock()) {
                    if (subItem.getQuantity() - oc.getReturnedQuantity() != subItem.getStockout()) {
                        return true;
                    }
                }
            }
        }
        return false;

    }
}
